﻿using System;
using System.Data;
using DALC4NET;
using Entity;
using NLog;
using System.Linq;
namespace DAL
{
    public class ManageArticls
    {
        #region Variables
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        DBHelper _dbHelper = null;
        #endregion

        #region Constracter
        public ManageArticls()
        {
            _dbHelper = DBHelper.GetInstance();
        }
        #endregion

        #region Methods---Admin
        public object AddNewArticls(Articls articls)
        {
            try
            {
                object rowsAffected = 0;
                DBParameter param1 = new DBParameter("@category_ID", articls.CategoryID);
                DBParameter param2 = new DBParameter("@articls_Title", articls.ArticlsTitle);
                DBParameter param3 = new DBParameter("@articls_SmallDescription", articls.ArticlsSmallDescription);
                DBParameter param4 = new DBParameter("@articls_FullDescription", articls.ArticlsFullDescription);
                DBParameter param5 = new DBParameter("@publisherName", articls.PublisherName);
                DBParameter param6 = new DBParameter("@articls_Thumb", articls.ArticlsThumb);
                DBParameter param7 = new DBParameter("@articls_Image", articls.ArticlsImage);
                DBParameter param8 = new DBParameter("@articls_Vedio", articls.ArticlsVedio);
                DBParameter param9 = new DBParameter("@articls_IsFeatures", articls.IsFeatures);
                DBParameter param10 = new DBParameter("@articls_Status", articls.ArticlsStatus);
                DBParameter param11 = new DBParameter("@id",0, DbType.Int32, ParameterDirection.Output);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                paramCollection.Add(param3);
                paramCollection.Add(param4);
                paramCollection.Add(param5);
                paramCollection.Add(param6);
                paramCollection.Add(param7);
                paramCollection.Add(param8);
                paramCollection.Add(param9);
                paramCollection.Add(param10);
                paramCollection.Add(param11);

                IDbCommand command = null;
                command = _dbHelper.GetCommand("sp_AddArticals", paramCollection, CommandType.StoredProcedure);
                command.ExecuteNonQuery();
                rowsAffected = _dbHelper.GetParameterValue(10, command);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::AddNewArticls:::" + ex.Message);
                return 0;
            }
        }
        public DataSet GetAllArticls()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetAllArticls", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetAllArticls:::" + ex.Message);
                throw;
            }
        }
        public DataSet GetPenddingClientArticls()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetPenddingClientArticls", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetPenddingClientArticls:::" + ex.Message);
                throw;
            }
        }
        public DataSet GetAcceptClientArticls()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetAcceptClientArticls", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetAcceptClientArticls:::" + ex.Message);
                throw;
            }
        }
        public int AcceptClientArticls(string sArticlsID)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@articls_ID", sArticlsID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                rowsAffected = _dbHelper.ExecuteNonQuery("sp_AcceptClientArticls", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::AcceptClientArticls:::" + ex.Message);
                return 0;
            }
        }

        public int SetArticlesToPandding(string sArticlsID)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@articls_ID", sArticlsID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                rowsAffected = _dbHelper.ExecuteNonQuery("sp_SetArticlesToPandding", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::SetArticlesToPandding:::" + ex.Message);
                return 0;
            }
        }

 


        public DataSet GetMyArticlesByUserID(string sUserID)
        {
            try
            {
                DBParameter param1 = new DBParameter("@userID", sUserID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetClientArticlsByUserID", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetMyArticlesByUserID:::" + ex.Message);
                throw;
            }
        }
        public DataSet GetAllArticlsAndroidApps()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetArticlsForAndroidApps", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetAllArticlsAndroidApps:::" + ex.Message);
                throw;
            }
        }
        public DataSet GetAllArticlsForRSSFeeds()
        {
            try
            {
                DataSet dsGetAllArticlsForRSSFeeds = _dbHelper.ExecuteDataSet("sp_GetAllArticlsForRSSFeeds", CommandType.StoredProcedure);
                return dsGetAllArticlsForRSSFeeds;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetAllArticlsForRSSFeeds:::" + ex.Message);
                throw;
            }
        }        
        public Articls GetArticlsInfo(string sArticlsID)
        {
            IDbConnection connection = _dbHelper.GetConnObject();
            IDataReader reader = null;
            Articls objArticls = new Articls();
            try
            {
                DBParameter param1 = new DBParameter("@articalsID", sArticlsID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);

                reader = _dbHelper.ExecuteDataReader("sp_GetArticalsByID", connection, paramCollection, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    objArticls.ArticlsTitle = reader["Articls_Title"].ToString();
                    objArticls.IsFeatures = Convert.ToInt16(reader["Articls_IsFeatures"].ToString());
                    objArticls.ArticlsSmallDescription = reader["Articls_SmallDescription"].ToString();
                    objArticls.ArticlsFullDescription = reader["Articls_FullDescription"].ToString();
                    objArticls.PublisherName = reader["PublisherName"].ToString();
                    objArticls.CategoryID = Convert.ToInt32(reader["Category_ID"].ToString());
                    objArticls.ArticlsVedio = reader["Articls_Vedio"].ToString();
                    objArticls.ArticlsCounter = Convert.ToInt32(reader["Articls_Counter"].ToString());
                    objArticls.ArticlsThumb = reader["Articls_Thumb"].ToString();
                    objArticls.ArticlsImage = reader["Articls_Image"].ToString();
                }
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetArticlsInfo:::" + ex.Message);
                objArticls = null;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                    connection.Dispose();
                }

                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
            return objArticls;
        }
        public int DeleteArticls(string sArticlsID)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@articls_ID", sArticlsID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                rowsAffected = _dbHelper.ExecuteNonQuery("sp_DeleteArticls", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::DeleteArticls:::" + ex.Message);
                return 0;
            }
        }
        public int EditArticlsInfo(Articls articls)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@articls_ID", articls.ArticlsID);
                DBParameter param2 = new DBParameter("@category_ID", articls.CategoryID);
                DBParameter param3 = new DBParameter("@articls_Title", articls.ArticlsTitle);
                DBParameter param4 = new DBParameter("@articls_SmallDescription", articls.ArticlsSmallDescription);
                DBParameter param5 = new DBParameter("@articls_FullDescription", articls.ArticlsFullDescription);
                DBParameter param6 = new DBParameter("@publisherName", articls.PublisherName);
                DBParameter param7 = new DBParameter("@articls_Thumb", articls.ArticlsThumb);
                DBParameter param8 = new DBParameter("@articls_Image", articls.ArticlsImage);
                DBParameter param9 = new DBParameter("@articls_Vedio", articls.ArticlsVedio);
                DBParameter param10 = new DBParameter("@articls_IsFeatures", articls.IsFeatures);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                paramCollection.Add(param3);
                paramCollection.Add(param4);
                paramCollection.Add(param5);
                paramCollection.Add(param6);
                paramCollection.Add(param7);
                paramCollection.Add(param8);
                paramCollection.Add(param9);
                paramCollection.Add(param10);
                rowsAffected = _dbHelper.ExecuteNonQuery("sp_EditArticlsInfo", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::EditArticlsInfo:::" + ex.Message);
                return 0;
            }
        }
        public Articls GetLastRowsInfo()
        {
            IDbConnection connection = _dbHelper.GetConnObject();
            IDataReader reader = null;
            Articls objArticls = new Articls();
            try
            {

                reader = _dbHelper.ExecuteDataReader("SP_GetLastRows", connection, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    objArticls.ArticlsID = Convert.ToInt32(reader["Articls_ID"].ToString());
                    objArticls.CategoryID = Convert.ToInt32(reader["Category_ID"].ToString());
                }
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetLastRowsInfo:::" + ex.Message);
                objArticls = null;
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                    connection.Dispose();
                }

                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
            return objArticls;
        }
        public DataSet ArticlsReport(DateTime dtFrom, DateTime dtTo)
        {
            try
            {
                DBParameter param1 = new DBParameter("@dateForm", dtFrom);
                DBParameter param2 = new DBParameter("@dateTo", dtTo);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);

                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_ArticlasReport", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::ArticlsReport:::" + ex.Message);
                throw;
            }
        }
        #endregion

        #region Methods----Client
        public DataSet GetQuickFeatureNews(int nCategoriesID)
        {
            try
            {
                DBParameter param1 = new DBParameter("@catgID", nCategoriesID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetFeatureNews", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetQuickTopOneNews:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetArticlsDataByArticlsID(int nArticlsID)
        {
            try
            {
                DBParameter param1 = new DBParameter("@articlsID", nArticlsID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetArticlsDataByArticlsID", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetArticlsDataByArticlsID:::" + ex.Message);
                throw;
            }
        }

        public int EditArticlsCounter(int nArticlsID, int nNewArticlsCounter)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@articlsID", nArticlsID);
                DBParameter param2 = new DBParameter("@articlesCount", nNewArticlsCounter++);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);

                rowsAffected = _dbHelper.ExecuteNonQuery("sp_ArticlesCounter", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::EditArticlsCounter:::" + ex.Message);
                return 0;
            }
        }

        public DataSet GetTopTenReleatedNews(int nCategoriesID)
        {
            try
            {
                DBParameter param1 = new DBParameter("@catgID", nCategoriesID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetTenRelatedNews", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetTopTenReleatedNews:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetTop4ListHomePageNewsLarg(int nCategoriesID)
        {
            try
            {
                DBParameter param1 = new DBParameter("@catgID", nCategoriesID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetTop4ListHomePageNews_Larg", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetTop4ListHomePageNewsLarg:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetTop4ListHomePageNewsSmall(int nCategoriesID)
        {
            try
            {
                DBParameter param1 = new DBParameter("@catgID", nCategoriesID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetTop4ListHomePageNews_Small", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetTop4ListHomePageNewsSmall:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetPopulerViewed()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetPopulerViewed", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetPopulerViewed:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetUserClientArticls()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetUserClientArticls", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetUserClientArticls:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetFeatureNewsTopSlider()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetFeatureNewsTopSlider", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetFeatureNewsTopSlider:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetAllCatigoryArticlsFull(int nCategoriesID,int nPageIndex,int nPageSize)
        {
            try
            {
                DBParameter param1 = new DBParameter("@catgID", nCategoriesID);
                DBParameter param2 = new DBParameter("@PageIndex", nPageIndex);
                DBParameter param3 = new DBParameter("@PageSize", nPageSize);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                paramCollection.Add(param3);
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetAllCatigoryArticlsFull", paramCollection, CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetTop4ListHomePageNewsSmall:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetShopPlayer()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetShopPlayer", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetShopPlayer:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetNewArticlsByDate()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("SP_GetNewArticlsByDate", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetNewArticlsByDate:::" + ex.Message);
                throw;
            }
        }

        public DataSet GetMarqueeData()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetMarqueeData", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetMarqueeData:::" + ex.Message);
                throw;
            }
        }
        #endregion
    }
}
